import {
	getJsapiTicket,
	getAgentJsapiTicket,
	getUserCorpIdAndAgentId,
	getBaseInfo
} from '@/api/index.js'
import {
	h5Url
	// ,corpId,
	// agentId
} from '@/config/app'
import * as ww from '@wecom/jssdk'

let AgentJsapi = ''
let Jsapi = ''
let corpId = ''
let agentId = ''

async function getConfig() {
	// let code = {
	// 	userInfoCode: ''
	// }
	// code.userInfoCode = uni.getStorageSync('userInfoCode')
	// if(!code.userInfoCode){
	// 	uni.navigateTo({
	// 		url:'/pages/login/auth'
	// 	})
	// 	return
	// }
	let res = await getBaseInfo()
	if (res.code == 200) {
		agentId =  res.user.agentId
		corpId = res.user.corpId
	} else {
		//console.log(res,code,'werwer')
		uni.showToast({
			title:res.msg
		})
	}
	//console.log(res, corpId, agentId, 'config');
}


async function jsapi() {
	let params = {
		url: h5Url,
		corp_id: corpId
	}
	let res = await getJsapiTicket(params)
	Jsapi = res.data
}
async function agenJstApi() {
	let params = {
		url: h5Url,
		corp_id: corpId
	}
	let res = await getAgentJsapiTicket(params)
	//console.log(res.data,'agenJstApi');
	AgentJsapi = res.data
}

async function wwRegister(jsApi) {
	return new Promise((resolve, reject) => {
		ww.register({
			// suiteId:'wx03555b37f0f48ef9',
			//corpId: 'wxe0f0b473eecd6a6d',
			corpId: corpId,
			agentId: agentId,
			jsApiList: jsApi,
			getConfigSignature(url) {
				console.log(this.Jsapi, 'Jsapi')
				return {
					timestamp: Jsapi.timestamp,
					nonceStr: Jsapi.nonceStr,
					signature: Jsapi.signature
				}
			},
			getAgentConfigSignature(url) {
				console.log(AgentJsapi, 'AgentJsapi')
				return {
					timestamp: AgentJsapi.timestamp,
					nonceStr: AgentJsapi.nonceStr,
					signature: AgentJsapi.signature
				}
			},
			onAgentConfigSuccess(res) {
				//alert(JSON.stringify(res))
				//alert(888)
			},
			onAgentConfigFail(res) {
				//console.log(JSON.stringify(res), 'onAgentConfigFail');
				//alert(JSON.stringify(res))
			},
			onAgentConfigComplete(res) {
				//alert(JSON.stringify(res)+jsApi)
			},
			onConfigSuccess(res) {
				//alert(JSON.stringify(res)+jsApi)
				console.log(res, '成功')
				//alert(JSON.stringify(res))
			},
			onConfigFail(res) {
				//alert()

				//alert(JSON.stringify(res))
				//alert('jj')
			},
			onConfigComplete(res) {
				//alert(JSON.stringify(res)+jsApi)
				//alert(JSON.stringify(res))
			}
		})
		resolve()
	})
}
async function register(jsApi) {
	await getConfig()
	await jsapi()
	await agenJstApi()
	//if(jsApi.isArray()){
	//await wwRegister(jsApi.join(','))
	//}else{
	await wwRegister(jsApi)
	//}

}
export default register